package com.boarsoft.boar.code.svc.biz.impl;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.boarsoft.boar.code.bean.biz.BeanMappingBiz;
import com.boarsoft.boar.code.entity.MethodInfo;
import com.boarsoft.boar.code.svc.biz.MethodInfoBiz;
import com.boarsoft.boar.code.svc.biz.MethodParamInfoBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("methodInfoBiz")
public class MethodInfoBizImpl extends SimpleBizImpl implements MethodInfoBiz {

	@Autowired
	MethodParamInfoBiz methodParamInfoBiz;
	@Autowired
	BeanMappingBiz beanMappingBiz;

	/**
	 * 保存method信息
	 * 
	 * @param methodInfo
	 *            方法信息
	 * @return 存储id
	 */
	@Transactional
	@Override
	public String save(MethodInfo methodInfo, String face) {
		if (Util.strIsEmpty(methodInfo.getId())) {
			return (String) dao.save(methodInfo);
		} else {
			return (String) this.dao.merge(methodInfo);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<MethodInfo> list(String faceId, String key, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(MethodInfo.class.getName());
		sb.append(" m where m.faceId = '").append(faceId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (m.code like '%").append(key);
			// sb.append("%' or s.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(m.id)".concat(sb.toString()));
		// if (Util.strIsNotEmpty(orderBy)) {
		// sb.append(" order by ").append(orderBy);
		// } else {
		// sb.append(" order by name");
		// }
		List<MethodInfo> lt = dao.list("select m".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<MethodInfo>(total, lt, pageNo, pageSize);
	}

	/**
	 * 查询方法ID
	 * 
	 * @param methodInfo
	 *            方法信息
	 * @return 方法id
	 */
	@Transactional(readOnly = true)
	@Override
	public String selectMethodId(MethodInfo methodInfo) {
		Query<MethodInfo> qry = this.dao.createQuery(MethodInfo.class,
				"from MethodInfo where face = :face and faceId = :faceId and code = :code");
		qry.setParameter("face", methodInfo.getFace());
		qry.setParameter("faceId", methodInfo.getFaceId());
		qry.setParameter("code", methodInfo.getCode());
		List<MethodInfo> methodInfos = qry.list();
		if (CollectionUtils.isEmpty(methodInfos)) {
			return null;
		}
		return methodInfos.get(0).getId();
	}

	/**
	 * 通过faceId删除MethodInfo
	 * 
	 * @param faceId
	 *            接口Id
	 */
	@Override
	@Transactional
	public void delete(String faceId) {
		Query<MethodInfo> qry = this.dao.createQuery(MethodInfo.class, "from MethodInfo where faceId = :faceId ");
		qry.setParameter("faceId", faceId);

		List<MethodInfo> methodInfos = qry.list();
		if (!CollectionUtils.isEmpty(methodInfos)) {
			for (MethodInfo mMethodInfo : methodInfos) {
				this.dao.delete(mMethodInfo);
			}
		}
	}

	/**
	 * bean类型名列表查询：/code/bean/mapping/add.do
	 */
	@Transactional(readOnly = true)
	@Override
	public List<MethodInfo> findMethodInfoByFaceId(String faceId) {
		String sql = "select r from MethodInfo r where 1=1".concat(" and faceId = '" + faceId + "'");
		return this.dao.list(sql);
	}

	@Override
	@Transactional
	public void deleteById(String id) {
		String w = String.format("and id='%s'", id);
		MethodInfo methodInfo = this.dao.find(MethodInfo.class, w);
		if (methodInfo != null) {
			this.dao.delete(methodInfo);
			methodParamInfoBiz.deleteByMethodId(methodInfo.getId());
			beanMappingBiz.deleteByType(methodInfo.getId());
		}

	}

}
